package controllers;

import java.util.List;

import models.Operator;
import models.Oplog;
import services.AuthorityService;
import services.BackInfoService;
import services.DeviceHealthService;
import services.DeviceService;
import utils.BizConstants;
import utils.ExcelUtil;
import utils.FileConfig;
import vos.BackInfoVo;
import vos.DeviceHealthSearchVo;
import vos.DeviceHealthVo;
import vos.DeviceSearchVo;
import vos.DeviceSickSearchVo;
import vos.DeviceVo;
import vos.ReturnCode;
import vos.ReturnListVo;

/**
 * 设备不健康统计
 * 
 * @ClassName: DeviceHealthController 
 * @author: wangsm
 * @date: 2016年6月8日 下午1:36:36
 */
public class DeviceHealthController extends BaseController {
	/**
	 * 设备不健康统计表
	 *
	 * @Title: devicehealth 
	 * @return: void
	 */
	public static void devicehealth(){
		render();
	}
	/**
	 * 对不健康信息进行统计
	 *
	 * @Title: CountDeviceHealth 
	 * @param page 页码
	 * @param rows 页数量
	 * @param sort 排序字段
	 * @param order 排序方式
	 * @param deviceid 设备id
	 * @param devicename 设备名称
	 * @param startdate 开始时间
	 * @param enddate 结束时间
	 * @return: void
	 */
	public static void CountDeviceHealth(int page,int rows,String sort,String order,
			String deviceid,String devicename,String startdate,String enddate){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		DeviceHealthSearchVo vo= new DeviceHealthSearchVo(page,rows,sort,order,deviceid,devicename,startdate,enddate);
		long total=DeviceHealthService.countDeviceHealth(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<BackInfoVo> lists=DeviceHealthService.listDeviceHealth(vo);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 设备不健康记录表
	 *
	 * @Title: devicesick 
	 * @return: void
	 */
	public static void devicesick(String menuid){
		Operator oper=getLoginUser();
		List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
		for(String auth : auths){
			renderArgs.put(auth, auth);
		}
		render();
	}
	/**
	 * 获取不健康列表
	 *
	 * @Title: getDeviceSick 
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param deviceid 设备编号
	 * @param shopid 门店编号
	 * @param dealerid 经销商编号
	 * @param dealername 经销商名称
	 * @param shopname 门店名称
	 * @param devicename 设备名称
	 * @param category 不健康类别
	 * @return: void
	 */
	public static void getDeviceSick(int page,int rows,String sort,String order,
			String deviceid,String shopid,String dealerid,String beginTime,String endTime,
			String dealername,String shopname,String devicename,Integer category){
		if(page==0){
			page=1;
		}
		if(rows==0){
			rows=BizConstants.pagesize;
		}
		DeviceSickSearchVo vo= new DeviceSickSearchVo(page,rows,sort,order,
				deviceid,devicename,shopid,shopname,dealerid,dealername,category,beginTime,endTime);
		long total=DeviceHealthService.countDeviceSick(vo);
		ReturnListVo listVo=new ReturnListVo();
		listVo.setTotal(total);
		List<DeviceHealthVo> lists=DeviceHealthService.listDeviceSick(vo);
		listVo.setRows(lists);
		renderJSON(listVo);
	}
	/**
	 * 导出设备不健康记录
	 *
	 * @Title: exportSickXls 
	 * @param deviceid
	 * @param shopid
	 * @param dealerid
	 * @param dealername
	 * @param shopname
	 * @param devicename
	 * @param category
	 * @return: void
	 */
	public static void exportSickXls(String deviceid,String shopid,String dealerid,String dealername,
			String shopname,String devicename,Integer category,String beginTime,String endTime){
		if(!checkAuthority("DEVICE_EXPORT")){
			renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
		}
		DeviceSickSearchVo vo= new DeviceSickSearchVo(
				deviceid,devicename,shopid,shopname,dealerid,dealername,category,beginTime,endTime);
		List<DeviceHealthVo> lists=DeviceHealthService.exportSickXls(vo);
		long y=System.currentTimeMillis();
		String fileName = "设备不健康信息"+y+".csv";
        response.contentType = "application/x-download";
        Operator oper=getLoginUser();
        Oplog oplog=new Oplog(oper.operatorid,"导出设备不健康信息数据","设备管理");
		oplog.save(oplog);
        renderBinary(ExcelUtil.buildExportFile(FileConfig.deviceSickXlsTemplate, lists), fileName);
	}
}
